<script setup>
import { ref, computed } from 'vue'
import { useRoute } from 'vue-router'
import { communityPosts } from '../../api/mockData'

const route = useRoute()
const postId = parseInt(route.params.id)


const post = computed(() => {
  return communityPosts.find(item => item.id === postId) || {}
})


const commentContent = ref('')
const submitComment = () => {
  if (!commentContent.value.trim()) return
  alert('评论功能将在后续实现')
  commentContent.value = ''
}
</script>

<template>
  <div class="community-detail-container">
    <el-card class="post-card">
      <!-- 帖子头部 -->
      <div class="post-header">
        <div class="post-user">
          <el-avatar :size="50" :src="post.authorAvatar"></el-avatar>
          <div class="post-user-info">
            <div class="post-username">{{ post.author }}</div>
            <div class="post-time">{{ post.time }}</div>
          </div>
        </div>
        <el-tag size="medium" :type="
          post.category === 'guide' ? 'success' :
          post.category === 'qa' ? 'warning' :
          post.category === 'share' ? 'info' : 'danger'
        ">
          {{ 
            post.category === 'guide' ? '攻略' :
            post.category === 'qa' ? '问答' :
            post.category === 'share' ? '分享' : '结伴'
          }}
        </el-tag>
      </div>

      <!-- 帖子标题和内容 -->
      <h1 class="post-title">{{ post.title }}</h1>
      <div class="post-content">{{ post.content }}</div>

      <!-- 帖子图片 -->
      <div class="post-images" v-if="post.images && post.images.length">
        <el-image 
          v-for="(image, index) in post.images" 
          :key="index" 
          :src="image" 
          :preview-src-list="post.images"
          fit="cover"
          class="post-image"
        ></el-image>
      </div>

      <!-- 帖子位置 -->
      <div class="post-location" v-if="post.location">
        <el-icon><Location /></el-icon> {{ post.location }}
      </div>

      <!-- 帖子操作 -->
      <div class="post-actions">
        <el-button type="primary" plain>
          <el-icon><ThumbsUp /></el-icon> 点赞 ({{ post.likes }})
        </el-button>
        <el-button type="warning" plain>
          <el-icon><Star /></el-icon> 收藏
        </el-button>
        <el-button type="info" plain>
          <el-icon><Share /></el-icon> 分享
        </el-button>
      </div>
    </el-card>

    <!-- 评论区 -->
    <el-card class="comment-card">
      <template #header>
        <div class="comment-header">
          <h3>评论 ({{ post.comments }})</h3>
        </div>
      </template>

      <!-- 评论表单 -->
      <div class="comment-form">
        <el-input
          v-model="commentContent"
          type="textarea"
          :rows="3"
          placeholder="写下你的评论..."
        />
        <el-button type="primary" @click="submitComment">发表评论</el-button>
      </div>

      <!-- 评论列表 -->
      <div class="comment-list">
        <el-empty v-if="!post.commentList || post.commentList.length === 0" description="暂无评论" />
        <div v-else v-for="(comment, index) in post.commentList" :key="index" class="comment-item">
          <div class="comment-user">
            <el-avatar :size="40" :src="comment.avatar"></el-avatar>
            <div class="comment-info">
              <div class="comment-username">{{ comment.username }}</div>
              <div class="comment-time">{{ comment.time }}</div>
            </div>
          </div>
          <div class="comment-content">
            {{ comment.content }}
          </div>
          <div class="comment-actions">
            <span class="comment-action">
              <el-icon><ThumbsUp /></el-icon> 点赞 ({{ comment.likes || 0 }})
            </span>
            <span class="comment-action">
              <el-icon><ChatRound /></el-icon> 回复
            </span>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.community-detail-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.post-card {
  margin-bottom: 20px;
}

.post-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.post-user {
  display: flex;
  align-items: center;
  gap: 15px;
}

.post-user-info {
  display: flex;
  flex-direction: column;
}

.post-username {
  font-weight: bold;
  font-size: 16px;
  color: #303133;
}

.post-time {
  font-size: 14px;
  color: #909399;
}

.post-title {
  font-size: 24px;
  margin: 0 0 20px;
  color: #303133;
}

.post-content {
  font-size: 16px;
  line-height: 1.6;
  color: #606266;
  margin-bottom: 20px;
  white-space: pre-line;
}

.post-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 20px;
}

.post-image {
  width: 200px;
  height: 200px;
  border-radius: 4px;
  object-fit: cover;
}

.post-location {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #909399;
  font-size: 14px;
  margin-bottom: 20px;
}

.post-actions {
  display: flex;
  gap: 15px;
  justify-content: center;
  padding-top: 20px;
  border-top: 1px solid #EBEEF5;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.comment-header h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.comment-form {
  margin-bottom: 30px;
}

.comment-form .el-button {
  margin-top: 10px;
  float: right;
}

.comment-list {
  margin-top: 20px;
}

.comment-item {
  padding: 15px 0;
  border-bottom: 1px solid #EBEEF5;
}

.comment-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.comment-info {
  display: flex;
  flex-direction: column;
}

.comment-username {
  font-weight: bold;
  color: #303133;
}

.comment-time {
  font-size: 12px;
  color: #909399;
}

.comment-content {
  color: #606266;
  line-height: 1.5;
  margin-bottom: 10px;
}

.comment-actions {
  display: flex;
  gap: 15px;
}

.comment-action {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #909399;
  font-size: 14px;
  cursor: pointer;
}

.comment-action:hover {
  color: #409EFF;
}

@media (max-width: 768px) {
  .post-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .post-images {
    justify-content: center;
  }
  
  .post-image {
    width: 100%;
    max-width: 300px;
    height: auto;
  }
  
  .post-actions {
    flex-direction: column;
  }
}
</style>